package com.yucquan.app.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.exteam.common.base.BaseFragmentActivity;
import com.yucquan.app.R;
import com.yucquan.app.UserApp;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 功能描述：首页
 *
 * @author yangz
 */
public class HomeFragActivity extends BaseFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_act);
        UserApp.getInstance().addActivity(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        baseController = new HomeFragController(this);//已登录
//        if (-1 != CommUtils.getPreferenceInt(Const.PREF_KEY_USERID, -1)) {
//            baseController = new HomeFragController(this);//已登录
//        } else {
//
//        }
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click();      //调用双击退出函数
        }
        return false;
    }

    /**
     * 双击退出函数
     */
    private Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit;
        if (!isExit) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            this.finishDataActivity();
            System.exit(0);
        }
    }
}
